<?php
class Setting extends CI_Controller {
    
	/**
	 * Function __construct().
	 * used for Initialize Setting cintroller
	 * @autor Biplab Biswas
	 * @date
	 */
	
	function __construct()
	{
		parent::__construct();
		$this->controller = 'settings';
		$this->functions->checkAuthAdmin($this->controller.'/');
		$this->load->model('ModelSetting');
		$this->controller = 'setting';
	}

	/**
	* Function index().
	* used for Global Setting
	* @autor Biplab Biswas
	* @date 
	*/
	
	function index()
	{
		redirect(base_url($this->controller."/global_setting/site_info"));
		return true;
	}

	
	function global_setting()
	{
		$this->functions->checkAuthAdmin($this->controller.'/global_setting/');

		$data['msg'] = "";
		$mode = $this->uri->segment(3);
		$data['mode'] = $mode;
		$data['global'] = $this->ModelSetting->getGlobalData();
		//pr($data['global']);
		$data['show_Image_Path'] = file_upload_base_url().'/logo_image/';
		
		$data['submit_link'] = base_url($this->controller.'/do_global_setting/'.$mode.'/');

		$data['succmsg'] = $this->nsession->userdata('succmsg');
		$data['errmsg'] = $this->nsession->userdata('errmsg');

		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");

		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'setting/global_setting';


		$element_data['menu'] = array();
		$element_data['main'] = $data;

		$this->layout->setLayout('layout_main_view');
		$this->layout->multiple_view($elements,$element_data);
	}
    
	
	
	function do_global_setting()
	{
		$this->functions->checkAuthAdmin($this->controller.'/global_setting/');
			$mode = $this->uri->segment(3);
			$data['mode'] = $mode;
			//pr($_POST);
			/*if($mode == 'site_info'){
			$this->form_validation->set_rules('image', 'image', 'callback_file_check');
			}
			
			if($mode == 'currency_conversion'){
			$this->form_validation->set_rules('global_currency_conversion_rate', 'rate', 'trim|required|decimal');
			}
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
			
		if($this->form_validation->run() == TRUE)
		{
			*/
		if($mode == 'site_info'){
			$data['logo'] = $this->input->request("previous_Image");
			
			if($_FILES['image']['name']!=''){
				$name = 'logo.png';
				$main_image_path = file_upload_absolute_path().'/logo_image/logo.png';
				unlink($main_image_path);
				$tmp_name = $_FILES["image"]["tmp_name"];
				$path = "../uploads/logo_image/";
				move_uploaded_file($tmp_name, "$path/$name");
				$data['logo'] = $name;
				
				
			}
		
			$data['global_site_name']			= $this->input->request("global_site_name");
			$data['global_site_url']		= $this->input->request("global_site_url");
			$data['global_meta_title']			= $this->input->request("global_meta_title");
			$data['global_meta_keywords']		= $this->input->request("global_meta_keywords");
			$data['global_meta_description']	= $this->input->request("global_meta_description");
			
			$data['global_facebook_url']		= $this->input->request("global_facebook_url");
			$data['fbappid']			= $this->input->request("fbappid"); 
			$data['fbappsecret']			= $this->input->request("fbappsecret");
			$data['global_twitter_url']		= $this->input->request("global_twitter_url");
			$data['global_google_plus_url']		= $this->input->request("global_google_plus_url");
			$data['global_pinterest_url']		= $this->input->request("global_pinterest_url");
			$data['global_youtube_url']		= $this->input->request("global_youtube_url");
			$data['global_telephone_number']		= $this->input->request("global_telephone_number");
			$data['global_fax']		= $this->input->request("global_fax");
			$data['global_mobile_number']		= $this->input->request("global_mobile_number");
			$data['global_store_timings']		= $this->input->request("global_store_timings");
			$data['global_address']		= $this->input->request("global_address");
			 } 
		
		if($mode == 'payment_info'){
		
			$data['global_auth_net_tran_key']	= $this->input->request("global_auth_net_tran_key");
			$data['global_auth_net_login_id']	= $this->input->request("global_auth_net_login_id");
			$data['global_auth_net_url']		= $this->input->request("global_auth_net_url");
			$data['global_auth_net_mode']		= $this->input->request("global_auth_net_mode");
		}
		
		if($mode == 'currency_conversion'){

			$data['global_currency_conversion_rate']	= $this->input->request("global_currency_conversion_rate");
		}			
		if($mode == 'email_info'){
		
			$data['global_web_admin_name']		= $this->input->request("global_web_admin_name");
			$data['global_webadmin_email']		= $this->input->request("global_webadmin_email");
			$data['global_email_signature']		= $this->input->request("global_email_signature");
		
		}
			
			$this->ModelSetting->updateGlobalSetting($data);
	
			$this->nsession->set_userdata('succmsg', "Configuration Updated Successfully");
			
			redirect(base_url($this->controller."/global_setting/".$mode));
			return true;
		
			
			/*}else{
				
				$this->global_setting();
			}
			*/
	}
	
	function email_setting()
	{
		$this->functions->checkAuthAdmin($this->controller.'/email_setting/');
		$config['base_url'] 				= base_url($this->controller."/email_setting/");
		$config['uri_segment']  		= 3;
		$config['num_links'] 			= 10;
		$config['page_query_string'] 	= false;
		$config['extra_params'] 		= "";
		$config['extra_params'] 		= "";
	
		$this->pagination->setAdminPaginationStyle($config);
		$start = 0;
	
		$data['controller'] = $this->controller;
	
		$param['sortType'] 		= $this->input->request('sortType','ASC');
		$param['sortField'] 		= $this->input->request('sortField','template_name');
		$param['searchField'] 		= $this->input->request('searchField','');
		$param['searchText'] 		= $this->input->request('searchText','');
		$param['searchDisplay'] 	= $this->input->request('searchDisplay','10');
	
		$data['recordset'] 			= $this->ModelSetting->getListEmailTemplate($config,$start,$param);
		$data['startRecord'] 		= $start;
	
		$this->pagination->initialize($config);
	
		$data['params'] 			= $this->nsession->userdata('ADMIN_EMAIL_TEMPLATE');
		$data['reload_link']      	= base_url($this->controller."/email_setting/");
		$data['add_link']        	= base_url($this->controller."/email_setting_addedit/0/0/");
		$data['edit_link']        	= base_url($this->controller."/email_setting_addedit/{{ID}}/0/");
		$data['delete_link']      	= base_url($this->controller."/email_setting_delete/{{ID}}/0/");
		$data['delete_all_link']	= $this->controller."/email_setting_delete_all/";
		$data['showall_link']     	= base_url($this->controller."/email_setting/0/1/");
		$data['total_rows']			= $config['total_rows'];
	
		$data['search_link'] 		= base_url($this->controller."/email_setting/0/1/");
	
		$data['succmsg']			= $this->nsession->userdata('succmsg');
		$data['errmsg']				= $this->nsession->userdata('errmsg');
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");
	
		$elements					= array();
		$elements['menu']			= 'menu/index';
		$elements['main']			= 'setting/email_setting';
		$element_data['menu']		= array();
		$element_data['main']		= $data;
	
		$this->layout->setLayout('layout_main_view');
		$this->layout->multiple_view($elements,$element_data);
	
	}
	
	/**
	 * Function email_setting().
	 * used for Email Setting
	 * @autor Biplab Biswas
	 * @modifier
	 * @date
	 */
	
	function email_setting_addedit()
	{
		$this->functions->checkAuthAdmin($this->controller.'/email_setting/');
		$startRecord  	= 0;
		$emailId 		= $this->uri->segment(3, 0);
		$page 			= $this->uri->segment(4, 0);
	
		if($page > 0)
			$startRecord = $page;
	
		$page = $startRecord;
	
	
		$data['controller'] 		= 	$this->controller;
		$data['action'] 			= "Add";
		$data['params']['page'] 	= $page;
		$data['do_addedit_link']	= base_url($this->controller."/do_email_setting_addedit/".$emailId."/".$page."/");
		$data['back_link']			= base_url($this->controller."/email_setting/");
	
		if($emailId > 0)
		{
			$data['emailId'] = $emailId;
			$data['action'] = "Edit";
			//=================prepare DATA ==================
			$rs = $this->ModelSetting->getSingleEmailTemplate($emailId);
			if(is_array($rs[0]))
			{
				foreach($rs[0] as $key => $val)
				{
					if(!is_numeric($key))
					{
						$data[$key] = $val;
					}
				}
			}
	        preg_match_all("#([^{{]+)}}#msi",$data['email_body'],$data['available_vairables']);
	    }else{
			$data['action'] = "Add";
			$data['template_name'] 		= $this->input->request('template_name');
			$data['template_code'] 		= $this->input->request('template_code');
			$data['from_name'] 			= $this->input->request('from_name');
			$data['from_email'] 		= $this->input->request('from_email');
			$data['subject'] 			= $this->input->request('subject');
			$data['email_body'] 		= $this->input->request('email_body');
			$data['available_vairables'] = array();
		}
	
		// ================================================================
		$fckeditorConfig = array(
				'instanceName' => 'email_body',
				'BasePath' => fckeditor_base_url().'fckeditor/',
				'ToolbarSet' => 'Default',
				'Width' => '800',
				'Height' => '400',
				'Value' => ''
		);
		$this->load->library('fckeditor', $fckeditorConfig);
		// ================================================================
	
		$data['succmsg'] = $this->nsession->userdata('succmsg');
		$data['errmsg'] = $this->nsession->userdata('errmsg');
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");
	
		$elements = array();
		$elements['menu'] 			= 'menu/index';
		$elements['main'] 			= 'setting/email_setting_add_edit';
		$element_data['menu']		= array();
		$element_data['main']		= $data;
	
		$this->layout->setLayout('layout_main_view');
		$this->layout->multiple_view($elements,$element_data);
	}
	
	/**
	 * Function do_email_setting_addedit().
	 * used for save Email Setting
	 * @autor Biplab Biswas
	 * @modifier
	 * @date
	 */
	
	function do_email_setting_addedit()
	{
		$this->functions->checkAuthAdmin($this->controller.'/email_setting/');
		$emailId = $this->uri->segment(3, 0);
		$page = $this->uri->segment(4, 0);
	
		$template_name		= $this->input->request('template_name');
		$template_code		= $this->input->request('template_code');
	
		$this->form_validation->set_rules('template_name', 'Template Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('from_name', 'From Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('from_email', 'From Email', 'trim|required|xss_clean');
		$this->form_validation->set_rules('template_code', 'Template Code', 'trim|required|xss_clean');
		$this->form_validation->set_rules('subject', 'Email Subject', 'trim|required|xss_clean');
	
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
	
		if($this->form_validation->run() == TRUE)
		{
			$isEmailTemplateExist = $this->functions->existRecords(EMAIL_TEMPLATE_SETTING,'template_code',$template_code,'id',$emailId);
			if($isEmailTemplateExist)
			{
				$this->nsession->set_userdata('errmsg', 'Email Template already exist');
				redirect(base_url($this->controller."/email_setting/"));
				return true;
			}else{
				$data['template_name'] 		= $this->input->request('template_name');
				$data['template_code'] 		= $this->input->request('template_code');
				$data['from_name'] 			= $this->input->request('from_name');
				$data['from_email'] 		= $this->input->request('from_email');
				$data['subject'] 			= $this->input->request('subject');
				$data['email_body'] 		= $this->input->request('email_body');
	
				if($emailId > 0)   //edit
				{
					$affected_row = $this->ModelSetting->editEmailTemplate($emailId,$data);
	
					$this->nsession->set_userdata('succmsg', 'Successfully Email Template Edited.');
					redirect(base_url($this->controller."/email_setting/"));
					return true;
				}else{
					$insert_id = $this->ModelSetting->addEmailTemplate($data);
	
					if($insert_id)
					{
						$this->nsession->set_userdata('succmsg', 'Successfully Email Template Added.');
						redirect(base_url($this->controller."/email_setting/"));
						return true;
					}
				}
			}
	
		}else{
			$this->email_setting_addedit();
		}
	}
	
	/**
	 * Function email_setting_delete().
	 * used for Delete single Email Setting
	 * @autor Biplab Biswas
	 * @modifier
	 * @date
	 */
	
	function email_setting_delete()
	{
		$this->functions->checkAuthAdmin($this->controller.'/email_setting/');
		$id = $this->uri->segment(3, 0);
		$this->ModelSetting->deleteEmailTemplate($id);
		$this->nsession->set_userdata('succmsg', 'Successfully Email Template Deleted.');
		redirect(base_url($this->controller."/email_setting/"));
		return true;
	}
	
	/**
	 * Function email_setting_delete_all().
	 * used for Delete All Email Seting
	 * @autor Biplab Biswas
	 * @modifier
	 * @date
	 */
	
	function email_setting_delete_all()
	{
		$this->functions->checkAuthAdmin($this->controller.'/email_setting/');
		$id_str = $this->input->request('check_ids',0);
		$id_arr = explode("^",$id_str);
		foreach($id_arr as $id){
			if($id<>'' && $id<>0)
				$this->ModelSetting->deleteEmailTemplate($id);
		}
		$this->nsession->set_userdata('succmsg', 'Successfully Selected Email Template Deleted.');
		redirect(base_url($this->controller."/email_setting/"));
		return true;
	}
	
	function file_check($str){
		//pr($_FILES);
		if($_FILES['image']['name'] != ''){
		$img = explode('.', $_FILES['image']['name']);
		$extention = strtolower($img[1]); 
			if ($extention == 'png')
			{
				
				return TRUE;
			}
			else
			{
				$this->form_validation->set_message('file_check', 'Logo %s Should be in Wright Format(png).');
				return FALSE;
			}
		}else{
			return TRUE;
		}
		
		
	}
}


?>